<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'west',split:true,title:'分类管理'" style="width:200px;">
				<ul id="class-tree" style="width: 200px;"></ul>
		</div>
		<div id="tree_edit_info" data-options="region:'center',border:true,title:'操作说明'">
				
		</div>
</div>
<div id="mm" class="easyui-menu" style="width:120px;display: none">
		<div onclick="rename()" data-options="iconCls:'icon-reload'">重命名</div>
		<div onclick="append()" data-options="iconCls:'icon-add'">添加子分类</div>
		<div onclick="removeit()" data-options="iconCls:'icon-remove'">移除分类</div>
		<div class="menu-sep"></div>
		<div onclick="queryfarmatit()" data-options="iconCls:'icon-edit'">编辑规格名</div>
		<div class="menu-sep"></div>
		<div onclick="querypropertyit()" data-options="iconCls:'icon-edit'">编辑属性名</div>
		<div class="menu-sep"></div>
		<div onclick="expand()">展开</div>
		<div onclick="collapse()">合并</div>
</div>

<script type="text/javascript">
$('#class-tree').tree({  
    url:'item/classList',
    method: 'get',
	animate: true,
	onContextMenu: function(e,node){
			e.preventDefault();
			$(this).tree('select',node.target);
			$('#mm').menu('show',{
				left: e.pageX,
				top: e.pageY
			});
	},
	/* onClick: function(node){
		if($('#class-tree').tree('isLeaf',node.target)){//判断该节点是不是子节点
			
		}else{
			$.messager.alert('温馨提示','你点击的不是叶子节点!','info');
		}
	}, */
	onAfterEdit:function(node){
		if(node.id==undefined){ //说明新建节点
			var parentId=$("#class-tree").tree('getParent',node.target).id;
			$.post("item/addClass",{parentId:parentId,cName:node.text},function(data){
				if(data.status==200){
					node.id=data.id;
					$.messager.show({
						title:'成功提示',
						msg:'节点添加成功',
						timeout:4000,
						showType:'slide'
					});
				}else{
					$.messager.alert('错误','节点添加失败!','error');
					$('#class-tree').tree('remove', node.target);
				}
			});
		}else{//更新节点
			var parentId=$("#class-tree").tree('getParent',node.target).id;
			$.post("item/updateClass",{id:node.id,cName:node.text,parentId:parentId},function(data){
				if(data.status==200){
					$.messager.show({
						title:'成功提示',
						msg:'节点更新成功',
						timeout:4000,
						showType:'slide'
					});
				}
			});
			
		}
	}
});
function append(){
	var t = $('#class-tree');
	var node = t.tree('getSelected');
	t.tree('append', {
		parent: (node?node.target:null),
		data: [{
			text: 'new item'
		}]
	});
}
function removeit(){
	var node = $('#class-tree').tree('getSelected');
	$.messager.confirm('警告', '你确定要移除"'+node.text+'"节点吗?', function(r){
		if (r){
			if(node.id==1){
				$.messager.alert('错误','默认节点不能移除!','error');
				return;
			}
			
			if($('#class-tree').tree('isLeaf',node.target)){//判断该节点是不是子节点
				var parentId=$("#class-tree").tree('getParent',node.target).id;
				$.post("item/removeClass",{parentId:parentId,id:node.id},function(data){
					if(data.status==200){
						$('#class-tree').tree('remove', node.target);
					}else{
						$.messager.alert('错误','节点移除失败!','error');
					}
				});
			}else{
				$.messager.alert('温馨提示','请先移除该节点下的所有子节点!','info');
			}
			
			
		}
	});
	
}
function collapse(){
	var node = $('#class-tree').tree('getSelected');
	$('#class-tree').tree('collapse',node.target);
}
function expand(){
	var node = $('#class-tree').tree('getSelected');
	$('#class-tree').tree('expand',node.target);
}

function rename(){
	var node = $('#class-tree').tree('getSelected');
	$('#class-tree').tree('beginEdit',node.target);
}
function queryfarmatit(){
	var node = $('#class-tree').tree('getSelected');
	if($('#class-tree').tree('isLeaf',node.target)){//判断该节点是不是子节点
		$('#tree_edit_info').panel({ 
			loadingMessage:'数据加载中...',
			 fit:true,
			  title: node.text+':规格名称',
			  href:"back/goods-format?"+node.id
			});  
	}else{
		$.messager.alert('温馨提示','非叶子节点不能编辑!','info');
	}
}
function querypropertyit(){
	var node = $('#class-tree').tree('getSelected');
	if($('#class-tree').tree('isLeaf',node.target)){//判断该节点是不是子节点
		$('#tree_edit_info').panel({ 
			loadingMessage:'数据加载中...',
			 fit:true,
			  title: node.text+':属性名称',
			  href:"back/goods-property?"+node.id
			});  
	}else{
		$.messager.alert('温馨提示','非叶子节点不能编辑!','info');
	}
}

</script>  
